layui.define(['jquery'],function (exports) {
    var $ = layui.jquery;
    var openParam;
    var base = {
        tableName: 'fullset',  // 存储表名
        xmSelectTheme: '#1E9FFF',
        getIP:function(){
            var ip=window.location.hostname;
            if(ip && ip.indexOf("localhost")>-1){
                ip="127.0.0.1";
            }
            return ip;
        },
        getPort:function(){
            var port=window.location.port;
            if(!port){
                port="8600";
            }
            return port;
        },
        getHost:function(){
            return window.location.host;
        },
        // 缓存临时数据
        putSessionData: function (key, value) {
            if (value) {
                layui.sessionData('tempData', {key: key, value: value});
            } else {
                layui.sessionData('tempData', {key: key, remove: true});
            }
        },
        // 获取缓存临时数据
        getSessionData: function (key) {
            return layui.sessionData('tempData')[key];
        },
        // 移除缓存临时数据
        removeSessionData: function (key) {
            layui.sessionData('tempData', {key: key, remove: true});
        },
        // 封装ajax请求，返回数据类型为json
        fsAjax: function (url, data, success) {
            // var token = config.getToken();
            // if (token) {
            //     data.access_token = token.access_token;
            // }
            base.ajax({
                url: url,
                data: data,
                type: 'post',
                dataType: 'json',
                contentType: "application/json",
                success: success,
                beforeSend: function (xhr) {
                    // var token = config.getToken();
                    // if (token) {
                    //     xhr.setRequestHeader('Authorization', 'Bearer ' + token.access_token);
                    // }
                }
            });
        },
        fsAsyncAjax: function (url, data, success, method) {
            // var token = config.getToken();
            // if (token) {
            //     data.access_token = token.access_token;
            // }
            base.asyncajax({
                url: url,
                data: data,
                type: 'post',
                dataType: 'json',
                contentType: "application/json",
                success: success,
                beforeSend: function (xhr) {
                    // var token = config.getToken();
                    // if (token) {
                    //     xhr.setRequestHeader('Authorization', 'bearer ' + token.access_token);
                    // }
                }
            });
        },
        // 封装异步ajax请求
        asyncAjax: function (param) {
            var successCallback = param.success;
            param.success = function (result, status, xhr) {
                // 判断登录过期和没有权限
                // var jsonRs;
                // if ('json' == param.dataType.toLowerCase()) {
                //     jsonRs = result;
                // } else if ('html' == param.dataType.toLowerCase() || 'text' == param.dataType.toLowerCase()) {
                //     jsonRs = admin.parseJSON(result);
                // }
                // if (jsonRs) {
                //     if (jsonRs.code == 401) {
                //         config.removeToken();
                //         layer.msg('登录过期', {icon: 2, time: 1500}, function () {
                //             location.replace('/login.html');
                //         }, 1000);
                //         return;
                //     } else if (jsonRs.code == 403) {
                //         layer.msg('没有权限', {icon: 2});
                //         return;
                //     }
                // }
                successCallback(result, status, xhr);
            };
            param.error = function (xhr) {
                param.success({code: xhr.status, msg: xhr.statusText});
            };
            //发送异步ajax请求
            param.async = true;
            $.ajax(param);
        },
        // 封装同步ajax请求
        ajax: function (param) {
            var successCallback = param.success;
            param.success = function (result, status, xhr) {
                // 判断登录过期和没有权限
                // var jsonRs;
                // if ('json' == param.dataType.toLowerCase()) {
                //     jsonRs = result;
                // } else if ('html' == param.dataType.toLowerCase() || 'text' == param.dataType.toLowerCase()) {
                //     jsonRs = admin.parseJSON(result);
                // }
                // if (jsonRs) {
                //     if (jsonRs.code == 401) {
                //         config.removeToken();
                //         layer.msg('登录过期', {icon: 2, time: 1500}, function () {
                //             location.replace('/login.html');
                //         }, 1000);
                //         return;
                //     } else if (jsonRs.code == 403) {
                //         layer.msg('没有权限', {icon: 2});
                //         return;
                //     }
                // }
                successCallback(result, status, xhr);
            };
            param.error = function (xhr) {
                param.success({code: xhr.status, msg: xhr.statusText});
            };
            //发送同步ajax请求
            param.async = false;
            $.ajax(param);
        },
        refresh: function () {
            Q.refresh();
        },
        // 封装layer.open
        open: function (param) {
            if(!param.id || param.id == ""){
                param.id = 'fsAdmin-pc';
            }
            openParam = param;
            var sCallBack = param.success;
            param.type = 1; //页面层
            param.area = param.area ? [param.area, $(document).height()+'px']: ['560px', $(document).height()+'px'];
            param.offset = param.offset ? param.offset : 'rt';
            param.resize ? param.resize : false;
            param.shade ? param.shade : .2;
            param.anim ? param.anim : 1;
            param.success = function (layero, index) {
                sCallBack ? sCallBack(layero, index) : '';
                $(layero).children('.layui-layer-content').load(param.path);
            };
            return layer.open(param);
        },
        // 封装layer.close，添加回调方法
        close: function (data) {
            layer.close(layer.index);
            openParam.finish ? openParam.finish(data) : '';
        },
        msgSuccess: function(msg){
            msg ? msg : '操作失败';
            layer.msg(msg, {icon: 1, time: 2000});
        },
        msgError: function(msg){
            msg ? msg : '操作失败';
            layer.msg(msg, {icon: 2, time: 2000});
        }
    };

    exports('base', base);
});
